//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "EuropeanExercise.h"
using namespace Cephei::QL;
#include <gen/QL/Exercise.h>
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::CEuropeanExercise::CEuropeanExercise (DateTime date) : CExercise(CEuropeanExercise::typeid)
{
    try
    {
#ifdef HANDLE
        _phEuropeanExercise = NULL;
#endif
        QuantLib::Date _date = (QuantLib::Date)ValueHelper::Convert (date); //d
        _ppEuropeanExercise = new boost::shared_ptr<QuantLib::EuropeanExercise> (new QuantLib::EuropeanExercise ( _date ));
        SetExercise (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEuropeanExercise));
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::CEuropeanExercise::CEuropeanExercise (boost::shared_ptr<QuantLib::EuropeanExercise>& childNative, Object^ owner) : CExercise(CEuropeanExercise::typeid)
{
#ifdef HANDLE
	_phEuropeanExercise = NULL;
#endif
	_ppEuropeanExercise = &childNative;
    _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEuropeanExercise));
}
Cephei::QL::CEuropeanExercise::CEuropeanExercise (QuantLib::EuropeanExercise& childNative, Object^ owner) : CExercise(CEuropeanExercise::typeid)
{
#ifdef HANDLE
	_phEuropeanExercise = NULL;
#endif
	_ppEuropeanExercise = new boost::shared_ptr<QuantLib::EuropeanExercise> (&childNative);
    _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEuropeanExercise));
    _EuropeanExerciseOwner = owner;
    _ExerciseOwner = owner;
}

Cephei::QL::CEuropeanExercise::CEuropeanExercise (CEuropeanExercise^ copy) : CExercise(CEuropeanExercise::typeid)
{
#ifdef HANDLE
	_phEuropeanExercise = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppEuropeanExercise = new boost::shared_ptr<QuantLib::EuropeanExercise> (copy->GetShared());
        _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEuropeanExercise));
    }
}
Cephei::QL::CEuropeanExercise::CEuropeanExercise (PLATFORM::Type^ t) : CExercise(CEuropeanExercise::typeid)
{
#ifdef HANDLE
	_phEuropeanExercise = NULL;
#endif
	if (!t->IsSubclassOf(CEuropeanExercise::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::CEuropeanExercise::CEuropeanExercise (QuantLib::Handle<QuantLib::EuropeanExercise>& childNative, Object^ owner)  : CExercise(CEuropeanExercise::typeid)
{
	_phEuropeanExercise = &childNative;
	_ppEuropeanExercise = &static_cast<boost::shared_ptr<QuantLib::EuropeanExercise>>(childNative.currentLink());
    _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEuropeanExercise));
    _EuropeanExerciseOwner = owner;
}
Cephei::QL::CEuropeanExercise::CEuropeanExercise (QuantLib::Handle<QuantLib::EuropeanExercise> childNative)  : CExercise(CEuropeanExercise::typeid)
{
	_phEuropeanExercise = &childNative;
	_ppEuropeanExercise = &static_cast<boost::shared_ptr<QuantLib::EuropeanExercise>>(childNative.currentLink());
    _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEuropeanExercise));
}
#endif
#ifdef STRUCT
Cephei::QL::CEuropeanExercise::CEuropeanExercise (QuantLib::EuropeanExercise childNative)  : CExercise(CEuropeanExercise::typeid)
{
#ifdef HANDLE
	_phEuropeanExercise = NULL;
#endif
	_ppEuropeanExercise = new boost::shared_ptr<QuantLib::EuropeanExercise> (new QuantLib::EuropeanExercise (childNative));
    _ppExercise = new boost::shared_ptr<QuantLib::Exercise> (boost::dynamic_pointer_cast<QuantLib::Exercise> (*_ppEuropeanExercise));
}
#endif

Cephei::QL::CEuropeanExercise::~CEuropeanExercise ()
{
    if (_ppEuropeanExercise != NULL)
    {
	    delete _ppEuropeanExercise;
        _ppEuropeanExercise = NULL;
    }
}
Cephei::QL::CEuropeanExercise::!CEuropeanExercise ()
{
    if (_ppEuropeanExercise != NULL)
    {
	    delete _ppEuropeanExercise;
    }
}
QuantLib::EuropeanExercise& Cephei::QL::CEuropeanExercise::GetReference ()
{
    if (_ppEuropeanExercise == NULL) throw REFNEW NativeNullException ();
	return **_ppEuropeanExercise;
}
boost::shared_ptr<QuantLib::EuropeanExercise>& Cephei::QL::CEuropeanExercise::GetShared ()
{
    if (_ppEuropeanExercise == NULL) throw REFNEW NativeNullException ();
	return *_ppEuropeanExercise;
}
QuantLib::EuropeanExercise* Cephei::QL::CEuropeanExercise::GetPointer ()
{
    if (_ppEuropeanExercise == NULL) throw REFNEW NativeNullException ();
	return &**_ppEuropeanExercise;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::EuropeanExercise>& Cephei::QL::CEuropeanExercise::GetHandle ()
{
	if (_phEuropeanExercise == NULL)
	{
		_phEuropeanExercise = new Handle<QuantLib::EuropeanExercise> (*_ppEuropeanExercise);
	}
	return *_phEuropeanExercise;
}
#endif
bool Cephei::QL::CEuropeanExercise::HasNative () 
{
	return (_ppEuropeanExercise != NULL);
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::IEuropeanExercise^ Cephei::QL::CEuropeanExercise_Factory::Create (DateTime date)
{
    return REFNEW CEuropeanExercise ( date);
}
